Date.prototype.format = function (format) {
  var o = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "h+": this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "q+": Math.floor((this.getMonth() + 3) / 3),
    S: this.getMilliseconds()
  }
  if (/(y+)/.test(format)) {
    format = format.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    )
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      )
    }
  }
  return format
}
/*
 *  pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
 */
function getFormatDateByLong(l, pattern) {
  return getFormatDate(new Date(l), pattern)
}
function getFormatDate(date, pattern) {
  if (date == undefined) {
    date = new Date()
  }
  if (pattern == undefined || pattern == "") {
    pattern = "yyyy-MM-dd hh:mm:ss"
  }
  return date.format(pattern)
}

//时间格式转换
export function formate(date) {
  if (!date) {
    return ""
  }
  var timeStamp = new Date().setHours(23, 59, 59, 0) / 1000 // 今天23:59:59秒数
  var eTime = 24 * 60 * 60 //一天时间秒数
  var tTime = new Date(date).getTime() / 1000 // 传来的时间秒数
  // var yTime = timeStamp - eTime;  //昨天23:59:59时间秒数
  // var byTime = timeStamp - 2 * eTime;  //前天23:59:59时间秒数
  var currentTime = timeStamp - tTime
  if (currentTime < eTime) {
    return getFormatDateByLong(new Date(date).getTime(), "hh:mm")
  } else if (currentTime >= eTime && currentTime < 2 * eTime) {
    return "昨天 " + getFormatDateByLong(new Date(date).getTime(), "hh:mm")
  } else if (currentTime >= 2 * eTime && currentTime < 7 * eTime) {
    let nowWeek = new Date(date).getDay()
    let arr = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
    let week_one = arr[nowWeek]
    return (
      week_one + " " + getFormatDateByLong(new Date(date).getTime(), "hh:mm")
    )
  } else {
    return getFormatDateByLong(new Date(date).getTime(), "yyyy/MM/dd hh:mm")
  }
}
